#include "../../../vcd-common-api/vcd-common.h"
#include "../setting-diagnosis/vcd-diagnosis.h"
#include "../setting-main/vcd-sys-upgrade.h"
#include "vcd-setting-close.h"
#include "vcd-common-api/vcd-conf/vcd-conf.h"

void btn_quit_sys_set(GtkWidget* dgt)
{
    stop_ping();
    stop_check_new_version_thread();
    gtk_widget_destroy(dgt);
    dgt = NULL;
    update_vcd_conf();
}

GtkWidget* get_setting_close_box(GtkWidget* parent)
{
    GtkWidget *buttonsysquit = NULL;
    GtkWidget *hboxdown_close = NULL;
    GtkWidget *fixed_down_close = NULL;
    GtkWidget *gridleftdown_close = NULL;

    // new box
    hboxdown_close  = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 31);
    g_return_val_if_fail(hboxdown_close != NULL, NULL);

    //new button
    buttonsysquit = gtk_button_new();
    if (buttonsysquit == NULL)
        goto fail;

    gtk_widget_set_size_request(buttonsysquit, 61, 35);
    gtk_button_set_relief(GTK_BUTTON(buttonsysquit), GTK_RELIEF_NONE);

    //new grid
    gridleftdown_close = gtk_grid_new();
    if (gridleftdown_close == NULL)
        goto fail;

    gtk_widget_set_size_request(gridleftdown_close, 98, 7);

    //new fixed
    fixed_down_close = gtk_fixed_new();
    if (fixed_down_close == NULL)
        goto fail;

    //place them tegother
    gtk_widget_set_size_request(fixed_down_close, 35, 48);
    gtk_fixed_put((GtkFixed *)fixed_down_close, buttonsysquit, 15, 6);
    gtk_grid_attach((GtkGrid *)gridleftdown_close, fixed_down_close, 0, 0, 1, 1);
    gtk_box_pack_start(GTK_BOX(hboxdown_close), gridleftdown_close, FALSE, FALSE, 0);

    g_signal_connect_swapped(G_OBJECT(buttonsysquit), "clicked", G_CALLBACK(btn_quit_sys_set), parent);

    set_css(buttonsysquit, "button{border-radius:0px;color:transparent}");
    set_img(buttonsysquit, "button", "sysquit-1.png");
    set_css(buttonsysquit, "button:hover{border-radius:0px;border:none;box-shadow:3px 3px 5px alpha(rgb(50,50,50),0.5) inset}");
    set_img(buttonsysquit, "button:hover", "sysquit-2.png");

    set_css(hboxdown_close, "box{border-radius:0px;background-color:rgb(255,255,255);"
            "border:none}");
    set_css(gridleftdown_close, "grid{border-radius:0px;background-color:rgb(50,50,50);"
            "border:none}");

    return hboxdown_close;

fail:
    if (gridleftdown_close) {
        gtk_widget_destroy(gridleftdown_close);
        gridleftdown_close = NULL;
    }

    if (buttonsysquit) {
        gtk_widget_destroy(buttonsysquit);
        buttonsysquit = NULL;
    }

    if (hboxdown_close) {
        gtk_widget_destroy(hboxdown_close);
        hboxdown_close = NULL;
    }
    return NULL;
}
